{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "65102716-6a98-4251-9bbc-bd341062e221",
   "metadata": {},
   "source": [
    "## GraphQL Agent Tool\n",
    "\n",
    "This example walks through two examples of connecting an Agent to a GraphQL server, one unauthenticated endpoint and one authenticated. To start, we initialize the OpenAI package with our api key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c6cb675-121b-4f47-b91d-7fcd6e746fd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup OpenAI Agent\n",
    "import openai\n",
    "\n",
    "openai.api_key = \"sk-your-key\"\n",
    "from llama_index.agent import OpenAIAgent"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b39e01e-23ce-4b35-87f9-040bd7f693d0",
   "metadata": {},
   "source": [
    "## Unauthenticated server\n",
    "\n",
    "Our first example is connecting to a server provided by Apollo as an introduction to GraphQL. It provides some data about SpaceX rockets and launches.\n",
    "\n",
    "To get started, we setup the url we want to query and some basic headers, then we ask the agent to execute a query against the server."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0992299-3f8e-4122-abb7-54e6ed06511b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: graphql_request with args: {\n",
      "  \"query\": \"query Ships { ships { id name type } }\",\n",
      "  \"variables\": {},\n",
      "  \"operationName\": \"Ships\"\n",
      "}\n",
      "Got output: {\"data\":{\"ships\":[{\"id\":\"5ea6ed2d080df4000697c901\",\"name\":\"American Champion\",\"type\":\"Tug\"},{\"id\":\"5ea6ed2d080df4000697c902\",\"name\":\"American Islander\",\"type\":\"Cargo\"},{\"id\":\"5ea6ed2d080df4000697c903\",\"name\":\"American Spirit\",\"type\":\"Cargo\"},{\"id\":\"5ea6ed2d080df4000697c904\",\"name\":\"A Shortfall of Gravitas\",\"type\":\"Barge\"},{\"id\":\"5ea6ed2e080df4000697c905\",\"name\":\"Betty R Gambarella\",\"type\":\"Tug\"},{\"id\":\"5ea6ed2e080df4000697c906\",\"name\":\"Elsbeth III\",\"type\":\"Tug\"},{\"id\":\"5ea6ed2e080df4000697c907\",\"name\":\"GO Ms Chief\",\"type\":\"High Speed Craft\"},{\"id\":\"5ea6ed2e080df4000697c908\",\"name\":\"GO Ms Tree\",\"type\":\"High Speed Craft\"},{\"id\":\"5ea6ed2e080df4000697c909\",\"name\":\"Shannon\",\"type\":\"Cargo\"},{\"id\":\"5ea6ed2e080df4000697c90a\",\"name\":\"GO Pursuit\",\"type\":\"Cargo\"},{\"id\":\"5ea6ed2f080df4000697c90b\",\"name\":\"GO Quest\",\"type\":\"Cargo\"},{\"id\":\"5ea6ed2f080df4000697c90c\",\"name\":\"Megan\",\"type\":\"Cargo\"},{\"id\":\"5ea6ed2f080df4000697c90d\",\"name\":\"HAWK\",\"type\":\"Tug\"},{\"id\":\"5ea6ed2f080df4000697c90e\",\"name\":\"Hollywood\",\"type\":\"Tug\"},{\"id\":\"5ea6ed2f080df4000697c90f\",\"name\":\"Just Read The Instructions 1\",\"type\":\"Barge\"},{\"id\":\"5ea6ed2f080df4000697c910\",\"name\":\"Just Read The Instructions 2\",\"type\":\"Barge\"},{\"id\":\"5ea6ed2f080df4000697c911\",\"name\":\"Kelly C\",\"type\":\"Tug\"},{\"id\":\"5ea6ed30080df4000697c912\",\"name\":\"NRC Quest\",\"type\":\"Cargo\"},{\"id\":\"5ea6ed30080df4000697c913\",\"name\":\"Of Course I Still Love You\",\"type\":\"Barge\"},{\"id\":\"5ea6ed30080df4000697c914\",\"name\":\"Pacific Freeedom\",\"type\":\"Tug\"},{\"id\":\"5ea6ed30080df4000697c915\",\"name\":\"Pacific Warrior\",\"type\":\"Tug\"},{\"id\":\"5ea6ed30080df4000697c916\",\"name\":\"RACHEL\",\"type\":\"Tug\"},{\"id\":\"5ee68c683c228f36bd5809b5\",\"name\":\"Finn Falgout\",\"type\":\"Tug\"},{\"id\":\"601742b20c87b90be7bb7e86\",\"name\":\"Lauren Foss\",\"type\":\"Tug\"},{\"id\":\"6059166413f40e27e8af34b6\",\"name\":\"Shelia Bordelon\",\"type\":\"Cargo\"},{\"id\":\"608c1a06cf7f3d6152666ad4\",\"name\":\"Mr. Jonah\",\"type\":\"Tug\"},{\"id\":\"60c8c7a45d4819007ea69871\",\"name\":\"HOS Briarwood\",\"type\":\"Cargo\"},{\"id\":\"614251b711a64135defb3654\",\"name\":\"Doug\",\"type\":\"Cargo\"},{\"id\":\"618fad7e563d69573ed8caa9\",\"name\":\"Bob\",\"type\":\"Cargo\"}]}}\n",
      "\n",
      "========================\n",
      "Here are the id, name, and type of the Ships:\n",
      "\n",
      "1. Ship ID: 5ea6ed2d080df4000697c901, Name: American Champion, Type: Tug\n",
      "2. Ship ID: 5ea6ed2d080df4000697c902, Name: American Islander, Type: Cargo\n",
      "3. Ship ID: 5ea6ed2d080df4000697c903, Name: American Spirit, Type: Cargo\n",
      "4. Ship ID: 5ea6ed2d080df4000697c904, Name: A Shortfall of Gravitas, Type: Barge\n",
      "5. Ship ID: 5ea6ed2e080df4000697c905, Name: Betty R Gambarella, Type: Tug\n",
      "6. Ship ID: 5ea6ed2e080df4000697c906, Name: Elsbeth III, Type: Tug\n",
      "7. Ship ID: 5ea6ed2e080df4000697c907, Name: GO Ms Chief, Type: High Speed Craft\n",
      "8. Ship ID: 5ea6ed2e080df4000697c908, Name: GO Ms Tree, Type: High Speed Craft\n",
      "9. Ship ID: 5ea6ed2e080df4000697c909, Name: Shannon, Type: Cargo\n",
      "10. Ship ID: 5ea6ed2e080df4000697c90a, Name: GO Pursuit, Type: Cargo\n",
      "11. Ship ID: 5ea6ed2f080df4000697c90b, Name: GO Quest, Type: Cargo\n",
      "12. Ship ID: 5ea6ed2f080df4000697c90c, Name: Megan, Type: Cargo\n",
      "13. Ship ID: 5ea6ed2f080df4000697c90d, Name: HAWK, Type: Tug\n",
      "14. Ship ID: 5ea6ed2f080df4000697c90e, Name: Hollywood, Type: Tug\n",
      "15. Ship ID: 5ea6ed2f080df4000697c90f, Name: Just Read The Instructions 1, Type: Barge\n",
      "16. Ship ID: 5ea6ed2f080df4000697c910, Name: Just Read The Instructions 2, Type: Barge\n",
      "17. Ship ID: 5ea6ed2f080df4000697c911, Name: Kelly C, Type: Tug\n",
      "18. Ship ID: 5ea6ed30080df4000697c912, Name: NRC Quest, Type: Cargo\n",
      "19. Ship ID: 5ea6ed30080df4000697c913, Name: Of Course I Still Love You, Type: Barge\n",
      "20. Ship ID: 5ea6ed30080df4000697c914, Name: Pacific Freeedom, Type: Tug\n",
      "21. Ship ID: 5ea6ed30080df4000697c915, Name: Pacific Warrior, Type: Tug\n",
      "22. Ship ID: 5ea6ed30080df4000697c916, Name: RACHEL, Type: Tug\n",
      "23. Ship ID: 5ee68c683c228f36bd5809b5, Name: Finn Falgout, Type: Tug\n",
      "24. Ship ID: 601742b20c87b90be7bb7e86, Name: Lauren Foss, Type: Tug\n",
      "25. Ship ID: 6059166413f40e27e8af34b6, Name: Shelia Bordelon, Type: Cargo\n",
      "26. Ship ID: 608c1a06cf7f3d6152666ad4, Name: Mr. Jonah, Type: Tug\n",
      "27. Ship ID: 60c8c7a45d4819007ea69871, Name: HOS Briarwood, Type: Cargo\n",
      "28. Ship ID: 614251b711a64135defb3654, Name: Doug, Type: Cargo\n",
      "29. Ship ID: 618fad7e563d69573ed8caa9, Name: Bob, Type: Cargo\n"
     ]
    }
   ],
   "source": [
    "from llama_index.tools.graphql.base import GraphQLToolSpec\n",
    "\n",
    "# Unauthenticated example against a public server\n",
    "url = \"https://spacex-production.up.railway.app/\"\n",
    "headers = {\n",
    "    \"content-type\": \"application/json\",\n",
    "}\n",
    "\n",
    "graphql_spec = GraphQLToolSpec(url=url, headers=headers)\n",
    "agent = OpenAIAgent.from_tools(\n",
    "    graphql_spec.to_tool_list(),\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "print(agent.chat(\"get the id, name and type of the Ships from the graphql endpoint\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbd6a409-472b-4136-912b-2112210c7daf",
   "metadata": {},
   "source": [
    "The Agent was able to form the GraphQL based on our instructions, and additionally provided some extra parsing and formatting for the data. Nice!\n",
    "\n",
    "## Authenticated Server\n",
    "\n",
    "The next example shows setting up authentication headers to hit a private server, representing a Shopify store that has opened up GraphQL access based on an admin API token. To get started with an example similar to this, see the shopify.ipynb notebook. You will also find a more detailed example of using the Schema Definition Language file to fully unlock the GraphQL API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98e45bed-c0ae-40ba-8504-904e56da502a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: graphql_request with args: {\n",
      "  \"query\": \"query Products {\\n  products(first: 3) {\\n    edges {\\n      node {\\n        id\\n        title\\n      }\\n    }\\n  }\\n}\",\n",
      "  \"variables\": {},\n",
      "  \"operationName\": \"Products\"\n",
      "}\n",
      "Got output: {\"data\":{\"products\":{\"edges\":[{\"node\":{\"id\":\"gid://shopify/Product/8432014819606\",\"title\":\"The Minimal Snowboard\"}},{\"node\":{\"id\":\"gid://shopify/Product/8432014852374\",\"title\":\"The Videographer Snowboard\"}},{\"node\":{\"id\":\"gid://shopify/Product/8432014917910\",\"title\":\"The Draft Snowboard\"}}]}},\"extensions\":{\"cost\":{\"requestedQueryCost\":5,\"actualQueryCost\":5,\"throttleStatus\":{\"maximumAvailable\":1000.0,\"currentlyAvailable\":995,\"restoreRate\":50.0}}}}\n",
      "========================\n",
      "The ID and title of the first 3 products from the GraphQL server are:\n",
      "\n",
      "1. Product ID: gid://shopify/Product/8432014819606, Title: The Minimal Snowboard\n",
      "2. Product ID: gid://shopify/Product/8432014852374, Title: The Videographer Snowboard\n",
      "3. Product ID: gid://shopify/Product/8432014917910, Title: The Draft Snowboard\n"
     ]
    }
   ],
   "source": [
    "# Authenticated example against a Shopify store\n",
    "\n",
    "url = \"https://your-store.myshopify.com/admin/api/2023-07/graphql.json\"\n",
    "headers = {\n",
    "    \"accept-language\": \"en-US,en;q=0.9\",\n",
    "    \"content-type\": \"application/json\",\n",
    "    \"X-Shopify-Access-Token\": \"your-admin-key\",\n",
    "}\n",
    "\n",
    "graphql_spec = GraphQLToolSpec(url=url, headers=headers)\n",
    "agent = OpenAIAgent.from_tools(\n",
    "    graphql_spec.to_tool_list(),\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "print(\n",
    "    agent.chat(\"get the id and title of the first 3 products from the graphql server\")\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
